﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;

namespace MyWeb.Admin.Usercontrols
{
    public partial class ucUserView : UserControl
    {
        private readonly UserService _userService = new UserService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            gvData.DataSource = _userService.User_GetByAll();
            gvData.DataBind();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/View.aspx?action=ucUserAddUpdate&in=1");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvData.Rows)
            {
                var chckDelete = (CheckBox) row.FindControl("chckSelect");
                if (chckDelete.Checked)
                {
                    var findControl = (HiddenField) row.FindControl("hdID");
                    Response.Redirect("~/Admin/View.aspx?action=ucUserAddUpdate&in=2&id=" + findControl.Value);
                }
            }
        }

        protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                if (e.Row.Cells[3].Text == "2")
                {
                    e.Row.Cells[3].Text = "Administrator";
                }
                else if (e.Row.Cells[3].Text == "1")
                {
                    e.Row.Cells[3].Text = "Quản lý";
                }
                else if (e.Row.Cells[3].Text == "0")
                {
                    e.Row.Cells[3].Text = "Khách hàng";
                }
            }
        }

        protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Status")
            {
                int Id = Int32.Parse(e.CommandArgument.ToString());
                DataTable dt = _userService.User_GetById(Id);
                bool status = Boolean.Parse(dt.Rows[0]["status"].ToString());
                if (status)
                {
                    _userService.User_ActiveStatus(Id, false);
                }
                else
                {
                    _userService.User_ActiveStatus(Id, true);
                }
                BindGrid();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvData.Rows)
            {
                if (((CheckBox) row.FindControl("chckSelect")).Checked)
                {
                    var id = (HiddenField) row.FindControl("hdID");
                    _userService.User_Delete(Int32.Parse(id.Value));
                }
            }
            BindGrid();
        }

        protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvData.PageIndex = e.NewPageIndex;
            BindGrid();
        }
    }
}